// -----------------------------------------------
// Author:     code34 nicolas_boiteux@yahoo.fr
// Edited by:  Neumatic
// Warcontext: Handler for all enemy units
// -----------------------------------------------	

private ["_vehicle"];

_vehicle = _this select 0;

_vehicle addEventHandler ["killed", {
	wcgarbage = _this spawn WC_fnc_garbagecollector; 
}];

//A3 TODO: Mando missile
if (isNil "mando_missile_init_done") then {
	_vehicle removeAllEventHandlers "HandleDamage";
};

_vehicle addEventHandler ["HandleDamage", {
	private ["_name","_gunner","_commander"];
	if (side (_this select 3) in [west, civilian]) then {
		if ((_this select 2) > wcdammagethreshold) then {
			(_this select 0) setHit [(_this select 1), (_this select 2)];
			(_this select 0) setDamage (damage (_this select 0) + (_this select 2));
			if (damage (_this select 0) > 0.9) then {
				(_this select 0) setDamage 1;
				wcnumberofkilledofmissionV = wcnumberofkilledofmissionV + 1;
				(_this select 0) removeAllEventHandlers "HandleDamage";
			};
		};
		_name = currentMagazine (_this select 3);
		_name = getText (configFile >> "CfgMagazines" >> _name >> "DisplayNameShort");
		if (_name != "SD") then {
			_gunner = gunner (_this select 0);
			_commander = commander (_this select 0);
			{
				_x reveal (_this select 3);
				_x doTarget (_this select 3);
				_x doFire (_this select 3);
			} foreach [_gunner, _commander];
		};
	};
}];

_vehicle addEventHandler ["Fired", {
	private ["_increase"];
	_increase = ceil(random 5);
	if (position _vehicle distance wcmissionposition < wcalertzonesize) then {
		if ((wcalert + _increase) < 101) then {
			wcalert = (wcalert + _increase);
		} else {
			wcalert = 100;
		};
	};
	(_this select 0) setVariable ["cible", assignedTarget (_this select 0), false];
}];

_vehicle addEventHandler ["FiredNear", {
	private ["_gunner","_commander"];
	if (side (_this select 1) in [west, civilian]) then {
		_gunner = gunner (_this select 0);
		_commander = commander (_this select 0);
		{
			_x reveal (_this select 1);
			_x doTarget (_this select 1);
			_x doFire (_this select 1);
		} foreach [_gunner, _commander];
	};
	// If vehicle fire itself, it puts itself as cible to search and destroy on its own position
	(_this select 0) setVariable ["cible", (_this select 0), false];
}];

wcvehicles = wcvehicles + [_vehicle];